﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Heroius.Indexer
{
    public partial class DiaBusy : Form
    {
        public DiaBusy(string key)
        {
            InitializeComponent();
            this.key = key;
            finishAct = new Action(() =>
            {
                this.DialogResult = actResult;
            });
        }

        string key;
        Task actask;
        Delegate finishAct;
        DialogResult actResult;

        private void DiaBusy_Load(object sender, EventArgs e)
        {
            actask = new Task(act);
            actask.Start();
        }

        void act()
        {
            try
            {
                var lic = ActivatorHelper.licmgr.ActivateProduct(key, 1, "hsite");
                if (lic != null)
                {
                    switch (lic.Status)
                    {
                        case Heroius.Activator.LicenseStatus.Expired:
                            MessageBox.Show(string.Format("证书已过期：{0}", lic.StatusReason));
                            actResult = System.Windows.Forms.DialogResult.Cancel;
                            break;
                        case Heroius.Activator.LicenseStatus.InternalError:
                            MessageBox.Show(string.Format("发生错误：{0}", lic.StatusReason));
                            actResult = System.Windows.Forms.DialogResult.Cancel;
                            break;
                        default:
                        case Heroius.Activator.LicenseStatus.Invalid:
                            MessageBox.Show(string.Format("证书无效：{0}", lic.StatusReason));
                            actResult = System.Windows.Forms.DialogResult.Cancel;
                            break;
                        case Heroius.Activator.LicenseStatus.Licensed:
                            ActivatorHelper.lic = lic;
                            actResult = System.Windows.Forms.DialogResult.OK;
                            break;
                        case Heroius.Activator.LicenseStatus.MachineHashMismatch:
                            MessageBox.Show(string.Format("设备不匹配：{0}", lic.StatusReason));
                            actResult = System.Windows.Forms.DialogResult.Cancel;
                            break;
                        case Heroius.Activator.LicenseStatus.NotFound:
                            MessageBox.Show(string.Format("未找到：{0}", lic.StatusReason));
                            actResult = System.Windows.Forms.DialogResult.Cancel;
                            break;
                        case Heroius.Activator.LicenseStatus.TrialVersion:
                            MessageBox.Show(string.Format("试用版：{0}", lic.StatusReason));
                            actResult = System.Windows.Forms.DialogResult.OK;
                            break;
                    }
                }
                else
                {
                    MessageBox.Show("激活失败，序列号无效");
                    actResult = System.Windows.Forms.DialogResult.Cancel;
                }
            }
            catch
            {
                MessageBox.Show("激活失败，请检查网络");
                actResult = System.Windows.Forms.DialogResult.Cancel;
            }
            finally
            {
                this.Invoke(finishAct);
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

    }
}
